import cv2
import matplotlib.pyplot as plt

# 读取彩色图像并转换为灰度图像
color_image = cv2.imread('./Photographer.png')
gray_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2GRAY)

# 使用Sobel算子进行水平滤波
sobel_x = cv2.Sobel(gray_image, cv2.CV_64F, 1, 0, ksize=3)
sobel_x = cv2.convertScaleAbs(sobel_x)  # 转换为无符号8位整型

# 使用Sobel算子进行垂直滤波
sobel_y = cv2.Sobel(gray_image, cv2.CV_64F, 0, 1, ksize=3)
sobel_y = cv2.convertScaleAbs(sobel_y)  # 转换为无符号8位整型

# 显示原始图像
plt.imshow(gray_image, cmap='gray')
plt.title('Original Image')
plt.axis('off')
plt.show()

# 显示处理后的图像
plt.imshow(sobel_x, cmap='gray')
plt.title('Horizontal Sobel Filter')
plt.axis('off')
plt.show()

# 显示处理后的图像
plt.imshow(sobel_y, cmap='gray')
plt.title('Vertical Sobel Filter')
plt.axis('off')
plt.show()
